package org.display.util
{
	import flash.filesystem.*;
	import flash.utils.*;
	
	import mx.controls.Alert;
	
	import org.data.*;
	import org.events.*;
	import org.net.*;
	import org.utils.*;
	
	public class AddMoreEvent
	{
		private var path:String=""
		private var type:String=""
		private var classname:String=""
		
		private var str:String = ""
		private var arr:Array = []
		
		public function AddMoreEvent(type:String,classname:String,path:String)
		{
			
			this.path=path
				
			this.type=type
				
			//第一个字母大写
			this.classname=StringControl.onewordUp(classname)
			
			if(type=="添加异步事件"){
				
				asynEvent("AsynEvent")
				
				//modifyCommandControl("AsynEvent")
				
				modifyCommand("AsynEvent")
				
			}else if(type=="添加功能事件"){
				
				funEvent("FunEvent")
				
				//modifyCommandControl("FunEvent")
				
				modifyCommand("FunEvent")
				
			}
			
			new CreateCommandControl()

		}
		
		function modifyCommandControl(sub:String){
			
			//D:\abcTest\org\display\proxy\TtViewProxy.as
			
			var arr=path.replace(/.*?(?=\\org\\)/gis,"").replace("\\display\\","\\business\\").split("\\")
			
			arr.shift()	
			var pop:String=arr.pop()
				
			arr[arr.length-1]=pop.replace("Proxy.as","")
				
			arr.push(classname+sub)
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				Global.commandControlSource=e
				
				
				var regstr:String="//AbcController.instance.registerCommand()"
				
				var newstr:String=regstr+"\n       AbcController.instance.registerCommand(\""+arr.join(".")+"\");"
				
				if(Global.commandControlSource.indexOf("AbcController.instance.registerCommand(\""+arr.join(".")+"\");")==-1)Global.commandControlSource=Global.commandControlSource.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)
					
				
				
				regstr="private static var _instance:CommandControl;"
				
				newstr="private var "+arr[arr.length-1]+"_Event_"+pop.replace("Proxy.as","")+":"+arr.join(".")+";\n       "+regstr
				
				if(Global.commandControlSource.indexOf("private var "+arr[arr.length-1]+"_Event_"+pop.replace("Proxy.as","")+":"+arr.join(".")+";\n       ")==-1)Global.commandControlSource=Global.commandControlSource.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)	
					
				
				regstr="//import"
				
				newstr=regstr+"\n          import "+arr.join(".")+";"
				
				if(Global.commandControlSource.indexOf("import "+arr.join("."))==-1)Global.commandControlSource=Global.commandControlSource.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)	


				setTimeout(FileControl.saveFile,100,path,Global.commandControlSource)
				
			}

			
			var path:String=Global.workspace+"\\org\\control\\"+Global.username+"\\CommandControl.as"
			
			
			new AbcHTTPServices(path,res)
			
		}
		
		function modifyCommand(sub:String){
			
			var arr=path.replace("\\display\\","\\business\\").replace(/.*?(?=\\org\\)/gis,"").split("\\")
			
			arr.shift()
				
			var pop:String = arr.pop()
				
			arr[arr.length-1] = pop.replace("Proxy.as","")
			
			arr.push(classname+sub)
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
				
				var regstr:String="//function"
				
				var newstr:String=regstr+"\n      public function "+arr[arr.length-1]+"(){ AbcEventDispatcher.instance.dispatcher(\""+arr.join(".")+"\",view,null);}"
				
				if(source.indexOf("public function "+arr[arr.length-1]+"(){ AbcEventDispatcher.instance.dispatcher(\""+arr.join(".")+"\",view,null);}")==-1)source=source.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)
				
				setTimeout(FileControl.saveFile,100,path,source)
				
			}
			
			new AbcHTTPServices(path,res)
			
		}
		
		private function asynEvent(sub:String){
			
			var arr:Array=path.replace("\\display\\","\\business\\").split("\\")
			var pop:String=arr.pop()
			arr[arr.length-1]=pop.replace("Proxy.as","")
			
			var commandPath:String=arr.join("\\")+"\\"+classname+sub+".as"
			
			
			if(File.desktopDirectory.resolvePath(commandPath).exists==false){
				
				FileControl.saveFile(commandPath,getCommandSource(sub))
				
			}else{
			
			   Alert.show("已经有同名事件函数！")
			}
		}
		
		private function funEvent(sub:String){
			
			var arr:Array=path.replace("\\display\\","\\business\\").split("\\")
			var pop:String=arr.pop()
			arr[arr.length-1]=pop.replace("Proxy.as","")
				
			var commandPath:String=arr.join("\\")+"\\"+classname+sub+".as"
			
			if(File.desktopDirectory.resolvePath(commandPath).exists==false){
				
				FileControl.saveFile(commandPath,getCommandSource(sub))
				
			}else{
				
				Alert.show("已经有同名事件函数！")
			}
			
		}
		
		function getCommandSource(suffix:String):String{
			
			var str:String = path.replace("\\display\\","\\business\\").replace(/.*?(?=\\org\\)/gis,"")
			var arr:Array =str.split("\\")
			arr.shift()
			var pop:String=arr.pop()
			arr[arr.length-1]=pop.replace("Proxy.as","")
			
			var commandSource:String=Global.commandSource
				
			if(type=="添加异步事件"){
				
				commandSource = Global.asynCommandSource
			
			}else if(type=="添加功能事件"){
			
				commandSource = Global.funCommandSource
					
			}

				
			commandSource=commandSource.replace(/<package>/gis,arr.join("."))
			
			commandSource=commandSource.replace(/<class>/gis,classname+suffix)
			commandSource=commandSource.replace(/<type>/gis,arr[arr.length-1])
				
			
			arr.splice(arr.length-1,0,"view")	
			commandSource=commandSource.replace(/<import>/gis,"import "+(arr.join(".")).replace(/\.business\./gis,".display."))
				
			return commandSource
			
		}
		
	}
}